//
//  MainViewController.m
//  UI09_5_Cell高度根据图片大小调整
//
//  Created by Scott on 15/5/8.
//  Copyright (c) 2015年 www.lanou3g.com 蓝欧3G. All rights reserved.
//

#import "MainViewController.h"
#import "MyCell_1.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *imageArr;

@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageArr = [NSMutableArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", nil];
    }
    return self;
}


- (void)dealloc
{
    [_imageArr release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"cell高度自动调整";
    
    [self addTableView];
    
    
}

- (void)addTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    
    [tableView release];

}

#pragma mark - 知识点1 根据图片大小调整Cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /* 调用自定义cell类方法, 计算高度 */
    return  [MyCell_1 heightWithImage:[self.imageArr objectAtIndex:indexPath.row]];
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imageArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s, %d", __func__, __LINE__);
    MyCell_1 *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[[MyCell_1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"] autorelease];
    }
    
    NSLog(@"%s, %d", __func__, __LINE__);
    cell.cellImage = [self.imageArr objectAtIndex:indexPath.row];
    return cell;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
